/**
 * Copyright (c) 2024-2025 National Fundamental Software of China Co., Ltd.
 *
 * You should have find a copy of NFSChina License. If not, please contact us
 * by email <os_support@nfschina>
 *
**/

import QtQuick 2.15
import QtQuick.Controls 2.15 as QQC2
import QtQuick.Layouts 1.15
import QtGraphicalEffects 1.0
import org.kde.kcm 1.6 
import org.kde.kirigami 2.20 as Kirigami
import org.kde.kquickcontrols 2.0 as KQuickControls
import org.kde.plasma.wallpapers.image 2.0 as Wallpaper
import org.kde.plasma.extras 2.0 as PlasmaExtras

Kirigami.Page {
    id: root
    property color cfg_Color
    property color cfg_ColorDefault
    property string cfg_Image
    property string cfg_ImageDefault
    property int cfg_FillMode
    property int cfg_FillModeDefault
    property int cfg_SlideshowMode
    property int cfg_SlideshowModeDefault
    property bool cfg_SlideshowFoldersFirst
    property bool cfg_SlideshowFoldersFirstDefault: false
    property bool cfg_Blur: true
    property bool cfg_BlurDefault
    property var cfg_SlidePaths: []
    property var cfg_SlidePathsDefault: []
    property int cfg_SlideInterval: 0
    property int cfg_SlideIntervalDefault: 0
    property var cfg_UncheckedSlides: []
    property var cfg_UncheckedSlidesDefault: []

    signal configurationChanged()

    implicitHeight: Kirigami.Units.gridUnit * 45
    implicitWidth: Kirigami.Units.gridUnit * 45

    property var wallpaper: kcm.wallpaperIntegration
    property var configDialog: kcm    

    title: i18nc("@title", "wallpaper") 
    ConfigModule.buttons: ConfigModule.NoAdditionalButton
     

    ColumnLayout {
        anchors.fill: parent
        anchors.margins:24
        anchors.topMargin:0
        spacing:18

        Rectangle {
            id:previewBg
            width:200
            height:120
            Layout.alignment: Qt.AlignHCenter
            radius: 8 
            Loader {
                id: previewImg 
                anchors.fill:parent
                anchors.margins:4           
                source: Qt.resolvedUrl(kcm.wallpaperMainFile)
            }            
        }        
        
        Loader {
            id: main
            Layout.fillHeight: true
            Layout.fillWidth: true
            source: Qt.resolvedUrl(kcm.wallpaperThumbnailsFile)   
        }     

//        RowLayout {
//            id: buttonsRow
//            Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
//            QQC2.Button {
//                icon.name: "list-add"
//                text: i18nd("kcm_wallpaper","Add Image…")
//                onClicked: imageWallpaper.showFileDialog();
//            }
//        }
    }
    
    onCfg_ImageChanged:{
        kcm.setWallpaper(cfg_Image);
        imageWallpaper.image = cfg_Image;
    }

    Wallpaper.ImageBackend {
        id: imageWallpaper
        configMap: wallpaper.configuration
        renderingMode: Wallpaper.ImageBackend.SingleImage
        onSettingsChanged: {
            saveConfig();
        }
    }

    function saveConfig() {
        imageWallpaper.wallpaperModel.commitAddition();
        imageWallpaper.wallpaperModel.commitDeletion();        
    }

    onConfigurationChanged: {
         saveConfig();
    }
}
